package com.tiger.cloud.service.crm;

import com.tiger.cloud.model.crm.UserCompanyInfo;
import com.tiger.cloud.model.crm.UserInfo;
import com.tiger.cloud.dao.crm.UserCompanyInfoDao;
import com.tiger.cloud.dao.crm.UserInfoDao;
import com.tiger.cloud.enums.DeleteEnum;
import com.tiger.cloud.form.CompanyForm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.exception.DBException;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;

/**
 * 用户企业信息
 */
@Service("userCompanyInfoService")
public class UserCompanyInfoService extends TigerBaseService<UserCompanyInfo> implements IUserCompanyInfoService{
    
    @Autowired
	private UserCompanyInfoDao userCompanyInfoDao;
    @Autowired
	private UserInfoDao userInfoDao;
    @Autowired
    private IUserInfoService userInfoService;

	@Override
	public MybatisTigerDao<UserCompanyInfo> getEntityDao() {
		return userCompanyInfoDao;
	}

	@Override
	public UserCompanyInfo register(CompanyForm companyForm) {
		UserInfo findUserInfo=new UserInfo();
		findUserInfo.setUserId(companyForm.getUserId());
		findUserInfo.setIsDelete(DeleteEnum.NOMAIL);
		UserInfo sUserInfo=userInfoService.findOne(findUserInfo);
		if(sUserInfo!=null){//修改
			sUserInfo.setUserName(companyForm.getUsername());
			sUserInfo.setHeadImage(companyForm.getHeadImage());
			if(userInfoDao.update(sUserInfo)>0){
				UserCompanyInfo findUserCompanyInfo=new UserCompanyInfo();
				findUserCompanyInfo.setUserId(sUserInfo.getUserId());
				findUserCompanyInfo.setUserInfoId(sUserInfo.getId());
				UserCompanyInfo userCompanyInfo=findOne(findUserCompanyInfo);
				if(userCompanyInfo!=null){
					userCompanyInfo.setCompanyAttachment(companyForm.getBusinessCard());
					userCompanyInfo.setCompanyName(companyForm.getCompanyName());
					userCompanyInfo.setDistrictId(companyForm.getDistrictId());
					userCompanyInfo.setProvinceId(companyForm.getProvinceId());
					userCompanyInfo.setCityId(companyForm.getCityId());
					userCompanyInfo.setProvinceName(companyForm.getProvinceName());
					userCompanyInfo.setDistrictName(companyForm.getDistrictName());
					userCompanyInfo.setCityName(companyForm.getCityName());
					if(userCompanyInfoDao.insert(userCompanyInfo)>0){
						return userCompanyInfo;
					}else{
						throw new DBException();
					}	
				}
			}
		}else{//添加
			UserInfo userInfo=new UserInfo();
			userInfo.setUserId(companyForm.getUserId());
			userInfo.setUserName(companyForm.getUsername());
			userInfo.setHeadImage(companyForm.getHeadImage());
			if(userInfoDao.insert(userInfo)>0){
				UserCompanyInfo userCompanyInfo=new  UserCompanyInfo();
				userCompanyInfo.setUserId(companyForm.getUserId());
				userCompanyInfo.setUserInfoId(userInfo.getId());
				userCompanyInfo.setCompanyAttachment(companyForm.getBusinessCard());
				userCompanyInfo.setCompanyName(companyForm.getCompanyName());
				userCompanyInfo.setDistrictId(companyForm.getDistrictId());
				userCompanyInfo.setProvinceId(companyForm.getProvinceId());
				userCompanyInfo.setCityId(companyForm.getCityId());
				userCompanyInfo.setProvinceName(companyForm.getProvinceName());
				userCompanyInfo.setDistrictName(companyForm.getDistrictName());
				userCompanyInfo.setCityName(companyForm.getCityName());
				if(userCompanyInfoDao.insert(userCompanyInfo)>0){
					return userCompanyInfo;
				}else{
					throw new DBException();
				}
			}
		}
		return null;
	}
    
}